package com.wonders.frame.common.Enum;

import com.wonders.frame.common.util.SpringContextUtil;
import com.wonders.frame.entity.AuthDic;
import com.wonders.frame.entity.AuthOrga;
import com.wonders.frame.entity.AuthPermission;
import com.wonders.frame.entity.Tree.AreasNodeData;
import com.wonders.frame.repository.AuthDicRepository;
import com.wonders.frame.repository.AuthOrgaRepository;
import com.wonders.frame.repository.AuthPermissionRepository;
import com.wonders.frame.service.HuiAreasService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public enum InitCommonSourceEnum {
    INSTANCE;

    private Map<String, Map<String, Object>> source;

    public Map<String,List<AuthDic>> dicMap ;

    private  List<AreasNodeData> areas;


    InitCommonSourceEnum(){
        source = new HashMap<String, Map<String, Object>>();
        dicMap = new HashMap<String,List<AuthDic>>();
        areas = new ArrayList<AreasNodeData>();
        initCommonSource();
    }

    /**
     * 一些前台经常使用的，但不会经常变化的资源
     * 初始化在内存中，减少对数据库的操作
     */
    private void initCommonSource(){
        initPermission();
        initOrga();
        initDic();
        initAreas();
        System.out.println("source init success!");
    }

    public void initAreas(){
        HuiAreasService huiAreasService = (HuiAreasService) SpringContextUtil.getBeanByClass(HuiAreasService.class);
        areas = huiAreasService.getAreasTree();
    }
    public void initPermission(){
        AuthPermissionRepository apRepo = (AuthPermissionRepository) SpringContextUtil.getBeanByClass(AuthPermissionRepository.class);
        List<AuthPermission> permissions =    apRepo.findAll();
        Map<String, Object> perMap = new HashMap<String, Object>();
        for(AuthPermission per : permissions){
            if(RemovedCodeEnum.AVAILABLE.getCode().equals(per.getRemoved())){
                perMap.put(per.getId(), per);
            }
        }
        source.put(CommonSourceKeyEnum.PERMISSION.name(), perMap);
    }

    public void initOrga(){
        AuthOrgaRepository aoRepo = (AuthOrgaRepository) SpringContextUtil.getBeanByClass(AuthOrgaRepository.class);
        List<AuthOrga> orgas =    aoRepo.findAll();
        Map<String, Object> orgaMap = new HashMap<String, Object>();
        for(AuthOrga orga : orgas){
            if(RemovedCodeEnum.AVAILABLE.getCode().equals(orga.getRemoved())){
                orgaMap.put(orga.getId(), orga);
            }
        }
        source.put(CommonSourceKeyEnum.ORGA.name(), orgaMap);
    }


    public void initDic(){
        AuthDicRepository authRepo = (AuthDicRepository) SpringContextUtil.getBeanByClass(AuthDicRepository.class);
        List<AuthDic> dicList = authRepo.getAllAuthDic();
        dicMap=new HashMap<String,List<AuthDic>>();
        for(AuthDic dic : dicList){
            if(dicMap.get(dic.getDicType())!=null){
                List<AuthDic> list = dicMap.get(dic.getDicType());
                list.add(dic);
                dicMap.put(dic.getDicType(), list);
            }else{
                List<AuthDic> list = new ArrayList<AuthDic>();
                list.add(dic);
                dicMap.put(dic.getDicType(), list);
            }

        }

    }


    public List<AreasNodeData> getAreas(){
        return areas;
    }

    public static Map<String, Object> getCommonSource(String key){
        return INSTANCE.source.get(key);
    }
}
